package gui.panels;

import application.UserLoggedManager;
import com.google.common.eventbus.Subscribe;
import contracts.responses.ForcedOfflineResponse;
import domain.entities.User;
import eventbus.events.UserLoginEvent;
import eventbus.events.UserLogoutEvent;
import gui.SubPanel;
import internal.UserLoggedInfo;
import network.websocket.ChannelManager;
import network.websocket.WSServerSender;
import util.DialogHelper;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;

public class OnlineUserSubPanel extends SubPanel {

    JTable table = new JTable();
    JScrollPane scrollPane = new JScrollPane(table);
    DefaultTableModel model = new DefaultTableModel();
    JButton kickOutBtn = new JButton("提出用户");
    JButton tabooBtn = new JButton("禁言");
    JButton unTabooBtn = new JButton("解除禁言");


    {
        Vector vName = new Vector();
        model.setColumnIdentifiers(vName);
        vName.add("用户id");
        vName.add("连接id");
        vName.add("用户名");
        vName.add("昵称");
        vName.add("是否禁言");
        vName.add("登录时间");
    }


    public OnlineUserSubPanel() {
        loadLoggedInfo();
        scrollPane.setBounds(100, 120, 800, 300);
        kickOutBtn.setBounds(100, 60, 100, 30);
        tabooBtn.setBounds(250, 60, 100, 30);
        unTabooBtn.setBounds(400, 60, 100, 30);
        this.add(scrollPane);
        this.add(kickOutBtn);
        this.add(tabooBtn);
        this.add(unTabooBtn);

        kickOutBtn.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            UserLoggedInfo info = getSelectedUserLoggedInfo();
            if (info == null) {
                DialogHelper.showWaringDialog("请选择一个用户");
                return;
            }
            if (DialogHelper.showConfirmDialog("踢出用户", "您要踢出用户[" + info.getUser().getUsername() + "]吗？")) {
                WSServerSender.send(info.getConnId(), new ForcedOfflineResponse("您已经被服务器踢下线"));
                if (ChannelManager.closeConnect(info.getConnId())) {
                    model.removeRow(selectedRow);
                    DialogHelper.showSuccessDialog("成功踢出用户");
                    return;
                }
                DialogHelper.showFailDialog("踢出用户失败");
            }
        });


        tabooBtn.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            UserLoggedInfo info = getSelectedUserLoggedInfo();
            if (info == null) {
                DialogHelper.showWaringDialog("请选择一个用户");
                return;
            }
            if (info.getIsTaboo()) {
                DialogHelper.showWaringDialog("该用户已经被禁言");
                return;
            }
            if (DialogHelper.showConfirmDialog("禁言用户", "禁言用户[" + info.getUser().getUsername() + "]吗？")) {
                info.setTaboo(true);
                model.setValueAt("是", selectedRow, 4);
                DialogHelper.showSuccessDialog("禁言成功");
            }
        });


        unTabooBtn.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            UserLoggedInfo info = getSelectedUserLoggedInfo();
            if (info == null) {
                DialogHelper.showWaringDialog("请选择一个用户");
                return;
            }
            if (!info.getIsTaboo()) {
                DialogHelper.showWaringDialog("该用户没有被禁言，无法解除");
                return;
            }
            if (DialogHelper.showConfirmDialog("解除禁言", "给用户[" + info.getUser().getUsername() + "]解除禁言吗？")) {
                info.setTaboo(false);
                model.setValueAt("否", selectedRow, 4);
                DialogHelper.showSuccessDialog("解除禁言成功");
            }
        });


    }


    UserLoggedInfo getSelectedUserLoggedInfo() {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            String connId = (String) table.getValueAt(selectedRow, 1);
            return UserLoggedManager.getUserLoggedInfo(connId);
        }
        return null;
    }


    void loadLoggedInfo() {
        clearTable();
        UserLoggedManager.getAllUserLoggedInfo().forEach(this::addUserLoggedInfoToTable);
    }


    void addUserLoggedInfoToTable(UserLoggedInfo info) {
        User u = info.getUser();
        Object[] obj = new Object[]{u.getId(), info.getConnId(), u.getUsername(), u.getNickname(), info.getIsTabooString(), info.getLoginTime()};
        model.addRow(obj);
        table.setModel(model);
    }


    void clearTable() {
        while (model.getRowCount() > 0) {
            model.removeRow(model.getRowCount() - 1);
        }
    }


    @Subscribe
    public void logout(UserLogoutEvent e) {
        loadLoggedInfo();
    }

    @Subscribe
    public void login(UserLoginEvent e) {
        loadLoggedInfo();
    }


}
